﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MeetingRoomBooking.Application.Common.Interfaces;
using MeetingRoomBooking.Domain.Entities;
using MeetingRoomBooking.Domain.Events;

namespace MeetingRoomBooking.Application.Users.Commands;
public class CreateUserCommand : IRequest<int>
{
    public required string UserName { get; set; }
    public required string Email { get; set; }
    public required string Role { get; set; }
    public required string Password { get; set; }
}

public class CreateUserCommandHander : IRequestHandler<CreateUserCommand, int>
{
    private readonly IApplicationDbContext _context;

    public CreateUserCommandHander(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<int> Handle(CreateUserCommand request, CancellationToken cancellationToken)
    {
        User user = new()
        {
            UserName = request.UserName,
            Email = request.Email,
            Password = request.Password,
            Role = request.Role
        };

        user.AddDomainEvent(new UserCreatedEvent(user));

        _context.Users.Add(user);

        await _context.SaveChangesAsync(cancellationToken);

        return user.Id;
    }
}
